package com.djsearch.adapter;


import java.util.List;

import com.djsearch.R;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.bean.SearchApp;
import com.djsearch.data.ImageManager;
import com.djsearch.data.RequestImage;
import com.djsearch.ui.WebViewActivity;
import com.djsearch.util.AsyncImageLoader;
import com.djsearch.util.AsyncImageLoader.ImageCallback;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 搜应用的列表适配器
 * @author chenyang2
 *
 */
public class SearchAppListViewAdapter extends BaseAdapter{

	private Context mContext;
	private List<SearchApp> searchAppList;
	private Bitmap mbitmap;
	public SearchAppListViewAdapter(Context context,List<SearchApp> list) {
		mContext = context;
		searchAppList = list;
		Bitmap bitmap = ImageManager.drawableToBitmap(mContext.getResources().getDrawable(R.drawable.taogou),150.0f);
		mbitmap = (ImageManager.resizeImage(bitmap, 150.0f));
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return searchAppList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return searchAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public void refreshData(List<SearchApp> list){
		
		searchAppList = list;
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if (convertView == null) {
			LayoutInflater layoutInflater = ((Activity) mContext).getLayoutInflater();
			convertView = layoutInflater.inflate(R.layout.search_app_item, parent, false);
			convertView.setTag(convertView);
		} else {
			convertView = (View) convertView.getTag();
		}
		
		
		Holder.appImg = (ImageView)convertView.findViewById(R.id.app_icon);
		Holder.appName = (TextView)convertView.findViewById(R.id.appName);
		Holder.appName.setText(searchAppList.get(position).getSoftName());
		RequestImage.getIntance(null, "Movie").download(searchAppList.get(position).getImage(), 
				Holder.appImg);
		Holder.downLoadAppBtn = (Button)convertView.findViewById(R.id.down_load);
		Holder.downLoadAppBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(mContext,WebViewActivity.class);
				intent.putExtra("url", searchAppList.get(position).getDownUrl());
				 mContext.startActivity(intent);
				((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});
		TextView giveAwayInfo = (TextView)convertView.findViewById(R.id.giveInfo);
		return convertView;
	}
	
	static class Holder {
		static ImageView appImg;
		static TextView appName;
		static Button downLoadAppBtn;
	}

}
